import axios from 'axios'

// 创建axios实例
const request = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 从localStorage获取token
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    const res = response.data
    // 如果返回的状态码不是200，说明接口请求有误
    if (res.code !== 200) {
      // token过期或无效
      if (res.code === 401) {
        // 清除本地token并跳转到登录页
        localStorage.removeItem('token')
        window.location.href = '/login'
      }
      return Promise.reject(new Error(res.message || '请求失败'))
    }
    return res
  },
  error => {
    console.error('请求错误', error.response || error)
    return Promise.reject(error)
  }
)

// API服务前缀
const AUTH_SERVICE = '/icoolkj-ms-auth'

/**
 * 用户名密码登录
 * @param {Object} data 登录信息
 * @returns {Promise}
 */
export function loginByUsername(data) {
  return request({
    url: `${AUTH_SERVICE}/public/login`,
    method: 'post',
    data: {
      ...data,
      loginType: 1, // 用户名密码登录
      userType: 0  // 默认普通用户
    }
  })
}

/**
 * 手机号验证码登录
 * @param {Object} data 登录信息
 * @returns {Promise}
 */
export function loginByPhone(data) {
  return request({
    url: `${AUTH_SERVICE}/public/login`,
    method: 'post',
    data: {
      ...data,
      loginType: 2, // 手机号验证码登录
      userType: 0  // 默认普通用户
    }
  })
}

/**
 * 邮箱验证码登录
 * @param {Object} data 登录信息
 * @returns {Promise}
 */
export function loginByEmail(data) {
  return request({
    url: `${AUTH_SERVICE}/public/login`,
    method: 'post',
    data: {
      ...data,
      loginType: 3, // 邮箱验证码登录
      userType: 0  // 默认普通用户
    }
  })
}

/**
 * 获取图形验证码
 * @returns {Promise}
 */
export function getCaptcha() {
  return request({
    url: `${AUTH_SERVICE}/public/captcha/getCode`,
    method: 'post',
    data: {
      grantType: "uuid"
    }
  })
}

/**
 * 获取短信验证码
 * @param {Object} data 包含手机号和图形验证码信息
 * @returns {Promise}
 */
export function getSmsCode(data) {
  return request({
    url: `${AUTH_SERVICE}/public/captcha/getSmsCode`,
    method: 'post',
    data: {
      grantType: "sms",
      phoneNumber: data.phoneNumber,
      captcha: data.captcha,
      uuid: data.uuid
    }
  })
}

/**
 * 获取邮箱验证码
 * @param {Object} data 包含邮箱和图形验证码信息
 * @returns {Promise}
 */
export function getEmailCode(data) {
  return request({
    url: `${AUTH_SERVICE}/public/captcha/getEmailCode`,
    method: 'post',
    data: {
      grantType: "email",
      email: data.email,
      captcha: data.captcha,
      uuid: data.uuid
    }
  })
}

/**
 * 退出登录
 * @returns {Promise}
 */
export function logout() {
  return request({
    url: `${AUTH_SERVICE}/logout`,
    method: 'post'
  })
}

/**
 * 获取用户信息
 * @returns {Promise}
 */
export function getUserInfo() {
  return request({
    url: `${AUTH_SERVICE}/user/info`,
    method: 'get'
  })
} 